﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class PickUpSystem
	{
		public PickUpSystem(UIDocument uiDocument)
		{
			this.buildingSystem = new BuildingSystem(0);
			this.doc = uiDocument.Document;
			this.searchSupplySystem = new SearchSystem(uiDocument, SearchSystemFilter.ByName);
			this.searchBackSystem = new SearchSystem(uiDocument, SearchSystemFilter.ByName);
		}

		public BuildingSystem PickUp(HYHeatingSystemOption option)
		{
			BuildingSystem result;
			try
			{
				SearchNode rootNode = this.GetRootNode(this.searchSupplySystem, "供水");
				SearchNode rootNode2 = this.GetRootNode(this.searchBackSystem, "回水");
				if (rootNode == null || rootNode2 == null)
				{
					result = null;
				}
				else
				{
					this.equipmentsId = this.GetAvailableEquipmentId(rootNode, rootNode2);
					Log.Info(new string[]
					{
						string.Format("End Equipment cout is {0}", this.equipmentsId.Count<ElementId>())
					});
					if (this.equipmentsId == null || this.equipmentsId.Count == 0)
					{
						result = null;
					}
					else
					{
						Log.Info(new string[]
						{
							string.Format("rootNode flow is {0},back flow is {1}", rootNode.CurFlow(this.equipmentsId), rootNode2.CurFlow(this.equipmentsId))
						});
						this.PickupMainPipe(rootNode, rootNode2);
						this.GetRisePipeSupplySystem();
						this.PickUpFloorAndIndoorSystem();
						Log.Info(new string[]
						{
							"pickup done!!!!\r\t......................................................................................."
						});
						this.buildingSystem.Update();
						result = this.buildingSystem;
					}
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information("暂时不支持当前的系统连接类型！");
				Log.Error(new string[]
				{
					ex.TargetSite.ToString() + "\n" + ex.Message + ex.StackTrace
				});
				result = null;
			}
			return result;
		}

		private SearchNode GetRootNode(SearchSystem searchSystem, string systemName)
		{
			string strA = string.Empty;
			int num = 1;
			while (string.Compare(strA, systemName) != 0)
			{
				if (num != 1)
				{
					YJKMessageBox.Information(string.Format("请选择{0}系统干管！", systemName));
				}
				if (!searchSystem.Run(string.Format("请点击要搜索的{0}干管开始端：", systemName)))
				{
					return null;
				}
				strA = searchSystem.RootNode.SystemClassName();
				num++;
			}
			foreach (SearchNode searchNode in searchSystem.RootNode.GetEndNodes())
			{
				if (searchNode.In.GetSystemClass() != searchSystem.RootNode.Out.GetSystemClass() && searchNode.Element is Pipe)
				{
					searchNode.PreviousNode.NextNode = null;
				}
			}
			return searchSystem.RootNode;
		}

		private List<ElementId> GetAvailableEquipmentId(SearchNode supply, SearchNode back)
		{
			List<ElementId> result;
			try
			{
				IEnumerable<ElementId> enumerable = from s in supply.GetEndNodes()
				select s.Id;
				Log.Info(new string[]
				{
					"Supply equipment num:" + enumerable.Count<ElementId>()
				});
				IEnumerable<ElementId> enumerable2 = from s in back.GetEndNodes()
				select s.Id;
				Log.Info(new string[]
				{
					"Back equipment num:" + enumerable2.Count<ElementId>()
				});
				result = enumerable.Intersect(enumerable2).ToList<ElementId>();
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message + "\n" + ex.StackTrace);
				result = null;
			}
			return result;
		}

		private void PickupMainPipe(SearchNode rootSupply, SearchNode rootBack)
		{
			this.GetMainPipe(rootSupply);
			this.GetMainPipe(rootBack);
			this.buildingSystem.ConnectType = this.GetConnectType(this.buildingSystem.SupplyPipes.Last<HyCalcPipe>(), this.buildingSystem.BackPipes.Last<HyCalcPipe>());
			if (this.buildingSystem.ConnectType == 0)
			{
				this.GetMainBackSystem();
			}
		}

		private void GetMainPipe(SearchNode rootNode)
		{
			if (rootNode.IsRiserPipe())
			{
				return;
			}
			bool flag = rootNode.SystemClassName() == "供水";
			int num = 1;
			while (!rootNode.NextNode.IsRiserPipe())
			{
				HyCalcPipe hyCalcPipe = this.EachMainPipe(ref rootNode, false);
				hyCalcPipe.Index = num++;
				hyCalcPipe.Name = string.Format("总{0}干管{1}", rootNode.SystemClassName(), hyCalcPipe.Index);
				if (flag)
				{
					this.buildingSystem.SupplyPipes.Add(hyCalcPipe);
				}
				else
				{
					this.buildingSystem.BackPipes.Add(hyCalcPipe);
				}
				Log.Info(new string[]
				{
					string.Concat(new object[]
					{
						"PickUp :",
						hyCalcPipe.Name,
						"   Element count:",
						hyCalcPipe.Elements.Count
					})
				});
			}
		}

		private void GetMainBackSystem()
		{
			HyCalcPipe hyCalcPipe = this.buildingSystem.BackPipes.Last<HyCalcPipe>();
			SearchNode nextNode = hyCalcPipe.EndNode.NextNode;
			hyCalcPipe.Add(new ValveAndFitting(nextNode.Element, ValveAndFittingType.Elbow));
			nextNode = nextNode.NextNode;
			hyCalcPipe.Add(new PipeLine(nextNode.Element as Pipe, nextNode.CurFlow(this.equipmentsId)));
			hyCalcPipe.EndNode = nextNode;
			Log.Info(new string[]
			{
				string.Format("{1} endNode Id is {0}", hyCalcPipe.EndNode.Id.IntegerValue, hyCalcPipe.Name)
			});
			Log.Info(new string[]
			{
				string.Concat(new object[]
				{
					"PickUp Pipe:",
					hyCalcPipe.Name,
					"   Element count:",
					hyCalcPipe.Elements.Count
				})
			});
			nextNode = nextNode.NextNode;
			int num = hyCalcPipe.Index + 1;
			HyCalcPipe hyCalcPipe2 = this.EachMainPipe(ref nextNode, true);
			hyCalcPipe2.Index = num++;
			hyCalcPipe2.Name = string.Format("总回水干管{0}", hyCalcPipe2.Index);
			this.buildingSystem.BackPipes.Add(hyCalcPipe2);
			Log.Info(new string[]
			{
				string.Concat(new object[]
				{
					"PickUp Pipe:",
					hyCalcPipe2.Name,
					"   Element count:",
					hyCalcPipe2.Elements.Count
				})
			});
			while (!nextNode.NextNode.IsRiserPipe())
			{
				HyCalcPipe hyCalcPipe3 = this.EachMainPipe(ref nextNode, false);
				hyCalcPipe3.Index = num++;
				hyCalcPipe3.Name = string.Format("总{0}干管{1}", nextNode.SystemClassName(), hyCalcPipe3.Index);
				this.buildingSystem.BackPipes.Add(hyCalcPipe3);
				Log.Info(new string[]
				{
					string.Concat(new object[]
					{
						"PickUp Pipe:",
						hyCalcPipe3.Name,
						"   Element count:",
						hyCalcPipe3.Elements.Count
					})
				});
			}
		}

		private HyCalcPipe EachMainPipe(ref SearchNode node, bool bSide = false)
		{
			HyCalcPipe hyCalcPipe = new HyCalcPipe();
			if (node.ConnectorCount == 3)
			{
				ValveAndFitting vf = new ValveAndFitting(node.Element, ValveAndFittingType.DirectTee);
				hyCalcPipe.Add(vf);
				node = (bSide ? node.BranchNode : node.NextNode);
			}
			while (node.ConnectorCount < 3 && !node.NextNode.IsRiserPipe())
			{
				this.NodeAddToPipe(ref hyCalcPipe, node);
				node = node.NextNode;
			}
			hyCalcPipe.EndNode = node.PreviousNode;
			Log.Info(new string[]
			{
				string.Format("hyPipe endNode Id is {0}", node.PreviousNode.Id.IntegerValue)
			});
			return hyCalcPipe;
		}

		private int GetConnectType(HyCalcPipe supplyPipe, HyCalcPipe backPipe)
		{
			return this.JustEndEquipments(supplyPipe.EndNode, backPipe.EndNode);
		}

		private int JustEndEquipments(SearchNode supplyNode, SearchNode backNode)
		{
			int result = 0;
			IEnumerable<ElementId> enumerable = from n in supplyNode.GetEndNodes()
			where this.equipmentsId.Contains(n.Id)
			select n.Id;
			IEnumerable<ElementId> enumerable2 = from n in backNode.GetEndNodes()
			where this.equipmentsId.Contains(n.Id)
			select n.Id;
			if (enumerable.Count<ElementId>() == enumerable2.Count<ElementId>() && enumerable.Intersect(enumerable2).Count<ElementId>() == enumerable.Count<ElementId>())
			{
				result = 1;
			}
			return result;
		}

		private void GetRisePipeSupplySystem()
		{
			for (int i = 1; i <= this.buildingSystem.SupplyPipes.Count; i++)
			{
				SearchNode nextNode = this.buildingSystem.SupplyPipes.At(i).EndNode.NextNode;
				RisePipeSystem risePipeSystem = new RisePipeSystem(i, 0);
				this.GetRiserPipe(nextNode, risePipeSystem, false);
				this.buildingSystem.Add(risePipeSystem);
				Log.Info(new string[]
				{
					string.Format("pickup {0},supply floor count:{1}...............", risePipeSystem.Name, risePipeSystem.SupplyPipes.Count)
				});
			}
			Log.Info(new string[]
			{
				"RisePipe Supply done!!!!!!!!................................................"
			});
			this.GetRiserPipeBackSystem();
		}

		private void GetRiserPipeBackSystem()
		{
			for (int i = 1; i <= this.buildingSystem.BackPipes.Count; i++)
			{
				int num = (this.buildingSystem.ConnectType == 0) ? (this.buildingSystem.SupplyPipes.Count + 1 - i) : i;
				SearchNode nextNode = this.buildingSystem.BackPipes.At(num).EndNode.NextNode;
				RisePipeSystem at = this.buildingSystem.GetAt(i);
				this.GetRiserPipe(nextNode, at, this.buildingSystem.ConnectType == 0 && num == 1);
				at.ConnectType = this.GetConnectType(at.SupplyPipes.Last<HyCalcPipe>(), at.BackPipes.Last<HyCalcPipe>());
				Log.Info(new string[]
				{
					string.Format("{0} connecttype is {1}", at.Name, at.ConnectType)
				});
				if (at.ConnectType == 0)
				{
					this.DealRiseBackSystem(nextNode, at, this.buildingSystem.ConnectType == 0 && num == 1);
				}
				Log.Info(new string[]
				{
					string.Format("pickup {0},back floor count:{1}..................", at.Name, at.BackPipes.Count)
				});
			}
			Log.Info(new string[]
			{
				"RisePipe Back done!!!!!!!!................................................\r\t"
			});
		}

		private void DealRiseBackSystem(SearchNode node, RisePipeSystem rise, bool bNext = false)
		{
			rise.BackPipes.Clear();
			ValveAndFitting vf = this.DealFirstNode(ref node, bNext);
			int num = 1;
			HyCalcPipe sameWayFirstRisePipe = this.GetSameWayFirstRisePipe(ref node);
			sameWayFirstRisePipe.Add(vf);
			sameWayFirstRisePipe.Index = num++;
			sameWayFirstRisePipe.Name = string.Format("{0}立管{1}", node.SystemClassName(), sameWayFirstRisePipe.Index);
			rise.BackPipes.Add(sameWayFirstRisePipe);
			Log.Info(new string[]
			{
				string.Format("{0} endNode is {1}", sameWayFirstRisePipe.Name, sameWayFirstRisePipe.EndNode.Id.IntegerValue)
			});
			while (!node.NextNode.IsHorizontalPipe() && node.NextNode != null)
			{
				HyCalcPipe hyCalcPipe = this.EachRiserPipe(ref node, num == 1);
				if (hyCalcPipe == null)
				{
					break;
				}
				hyCalcPipe.Index = num++;
				hyCalcPipe.Name = string.Format("{0}立管{1}", node.SystemClassName(), hyCalcPipe.Index);
				rise.BackPipes.Add(hyCalcPipe);
				Log.Info(new string[]
				{
					string.Concat(new object[]
					{
						"PickUp Pipe:",
						hyCalcPipe.Name,
						"  Element count:",
						hyCalcPipe.Elements.Count
					})
				});
			}
		}

		private HyCalcPipe GetSameWayFirstRisePipe(ref SearchNode node)
		{
			HyCalcPipe hyCalcPipe = new HyCalcPipe();
			hyCalcPipe.Add(new PipeLine(node.Element as Pipe, node.CurFlow(this.equipmentsId)));
			node = node.NextNode;
			if (node.ConnectorCount == 2)
			{
				hyCalcPipe.Add(new ValveAndFitting(node.Element, ValveAndFittingType.Elbow));
				node = node.NextNode;
			}
			else if (node.ConnectorCount == 3)
			{
				hyCalcPipe.Add(new ValveAndFitting(node.Element, ValveAndFittingType.SideTee));
				node = node.BranchNode;
			}
			hyCalcPipe.Add(new PipeLine(node.Element as Pipe, node.CurFlow(this.equipmentsId)));
			node = node.NextNode;
			hyCalcPipe.Add(new ValveAndFitting(node.Element, ValveAndFittingType.Elbow));
			node = node.NextNode;
			hyCalcPipe.Add(new PipeLine(node.Element as Pipe, node.CurFlow(this.equipmentsId)));
			hyCalcPipe.EndNode = node;
			node = node.NextNode;
			return hyCalcPipe;
		}

		private ValveAndFitting DealFirstNode(ref SearchNode node, bool bNext = false)
		{
			ValveAndFitting valveAndFitting = new ValveAndFitting();
			valveAndFitting.Element = node.Element;
			int connectorCount = node.ConnectorCount;
			if (connectorCount != 2)
			{
				if (connectorCount == 3)
				{
					valveAndFitting.ValveType = (bNext ? ValveAndFittingType.DirectTee : ValveAndFittingType.SideTee);
					node = (bNext ? node.NextNode : node.BranchNode);
				}
			}
			else
			{
				valveAndFitting.ValveType = ValveAndFittingType.Elbow;
				node = node.NextNode;
			}
			return valveAndFitting;
		}

		private void GetRiserPipe(SearchNode node, RisePipeSystem rise, bool bNext = false)
		{
			ValveAndFitting vf = this.DealFirstNode(ref node, bNext);
			bool flag = node.SystemClassName() == "供水";
			int num = 1;
			while (!node.NextNode.IsHorizontalPipe() && node.NextNode != null)
			{
				HyCalcPipe hyCalcPipe = this.EachRiserPipe(ref node, num == 1);
				if (hyCalcPipe == null)
				{
					break;
				}
				if (num == 1)
				{
					hyCalcPipe.Add(vf);
				}
				hyCalcPipe.Index = num++;
				hyCalcPipe.Name = string.Format("{0}立管{1}", node.SystemClassName(), hyCalcPipe.Index);
				if (flag)
				{
					rise.SupplyPipes.Add(hyCalcPipe);
				}
				else
				{
					rise.BackPipes.Add(hyCalcPipe);
				}
			}
		}

		private HyCalcPipe EachRiserPipe(ref SearchNode node, bool bFirst = false)
		{
			HyCalcPipe hyCalcPipe = new HyCalcPipe();
			if (!bFirst)
			{
				this.NodeAddToPipe(ref hyCalcPipe, node, false);
				node = node.NextNode;
			}
			while (node.ConnectorCount < 3)
			{
				if (!this.NodeAddToPipe(ref hyCalcPipe, node) || node.NextNode == null)
				{
					return null;
				}
				Log.Info(new string[]
				{
					"pipes num is : " + hyCalcPipe.PipesLines.Count
				});
				Log.Element(node.Element);
				node = node.NextNode;
			}
			if (node.NextNode == null)
			{
				hyCalcPipe.EndNode = node;
			}
			else
			{
				hyCalcPipe.EndNode = node.PreviousNode;
			}
			Log.Info(new string[]
			{
				string.Format("riser hyPipe endNode Id is {0}", node.PreviousNode.Id.IntegerValue)
			});
			return hyCalcPipe;
		}

		private void PickUpFloorAndIndoorSystem()
		{
			for (int i = 1; i <= this.buildingSystem.RisePipes.Count; i++)
			{
				RisePipeSystem at = this.buildingSystem.GetAt(i);
				int connectType = at.ConnectType;
				Log.Info(new string[]
				{
					string.Format("{0} start PickUp floors.....", at.Name)
				});
				for (int j = 1; j <= at.SupplyPipes.Count; j++)
				{
					Log.Info(new string[]
					{
						"floor{0}  start pickup loop.....".Format(new object[]
						{
							j
						})
					});
					HyCalcPipe hyCalcPipe = at.SupplyPipes.At(j);
					int index = (connectType == 0) ? (at.SupplyPipes.Count + 1 - j) : j;
					HyCalcPipe hyCalcPipe2 = at.BackPipes.At(index);
					FloorSystem floorSystem = this.PickUpFloorSystem(hyCalcPipe.EndNode, hyCalcPipe2.EndNode);
					floorSystem.Index = j;
					at.Add(floorSystem);
					Log.Info(new string[]
					{
						string.Format("!!!!!{3} {0} indoor count is {1},Height is {2}\r\t...........................................................", new object[]
						{
							floorSystem.Name,
							floorSystem.InDoorSystem.Count,
							floorSystem.Height,
							at.Name
						})
					});
				}
				Log.Info(new string[]
				{
					string.Format("{0} floor count is {1}............\r\t", at.Name, at.FloorSystems.Count)
				});
			}
		}

		private FloorSystem PickUpFloorSystem(SearchNode supplyNode, SearchNode backNode)
		{
			FloorSystem floorSystem = new FloorSystem();
			SearchNode nextNode = supplyNode.NextNode;
			if (nextNode.ConnectorCount == 4)
			{
				IndoorSystem indoorSystem = this.PickUpIndoorSystem(nextNode.BranchNode, backNode);
				floorSystem.Add(indoorSystem);
				Log.Info(new string[]
				{
					string.Format("4-1!!!!{0} indoorLoop count is {1}..........!!!!", indoorSystem.Name, indoorSystem.Loops.Count)
				});
				IndoorSystem indoorSystem2 = this.PickUpIndoorSystem(nextNode.Branch2Node, backNode);
				indoorSystem2.Index = 2;
				floorSystem.Add(indoorSystem2);
				Log.Info(new string[]
				{
					string.Format("4-2!!!!{0} indoorLoop count is {1}..........!!!!", indoorSystem2.Name, indoorSystem2.Loops.Count)
				});
			}
			else if (nextNode.ConnectorCount == 3)
			{
				IndoorSystem indoorSystem3 = this.PickUpIndoorSystem(nextNode.BranchNode, backNode);
				floorSystem.Add(indoorSystem3);
				Log.Info(new string[]
				{
					string.Format("3-1!!!!{0} indoorLoop count is {1}..........!!!!", indoorSystem3.Name, indoorSystem3.Loops.Count)
				});
				if (nextNode.NextNode.IsHorizontalPipe())
				{
					IndoorSystem indoorSystem4 = this.PickUpIndoorSystem(nextNode.NextNode, backNode);
					indoorSystem4.Index = 2;
					floorSystem.Add(indoorSystem4);
					Log.Info(new string[]
					{
						string.Format("3-2!!!!{0} indoorLoop count is {1}..........!!!!", indoorSystem4.Name, indoorSystem4.Loops.Count)
					});
				}
			}
			else
			{
				IndoorSystem indoorSystem5 = this.PickUpIndoorSystem(nextNode.NextNode, backNode);
				floorSystem.Add(indoorSystem5);
				Log.Info(new string[]
				{
					string.Format("2!!!!{0} indoorLoop count is {1}..........!!!!", indoorSystem5.Name, indoorSystem5.Loops.Count)
				});
			}
			floorSystem.Height = this.CalcFloorHeight(nextNode, backNode.NextNode);
			return floorSystem;
		}

		private double CalcFloorHeight(SearchNode supply, SearchNode back)
		{
			double num = this.GetElementOffset(supply) - this.GetElementOffset(this.buildingSystem.SupplyPipes.At(1).EndNode.NextNode);
			double num2 = this.GetElementOffset(back) - this.GetElementOffset(this.buildingSystem.BackPipes.At(1).EndNode.NextNode);
			return 0.0005 * (num + num2);
		}

		private double GetElementOffset(SearchNode node)
		{
			double num = (node.Element.Document.GetElementById(node.Element.LevelId) as Level).Elevation * 304.8;
			return node.Element.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).AsDouble() * 304.8 + num;
		}

		private IndoorSystem PickUpIndoorSystem(SearchNode supplyNode, SearchNode backNode)
		{
			IndoorSystem indoorSystem = new IndoorSystem(1);
			SearchNode loopBackNode = this.GetLoopBackNode(supplyNode, backNode);
			if (loopBackNode == null)
			{
				return null;
			}
			Log.Info(new string[]
			{
				string.Format("supplyNode Id is {0},backNode Id is {1}", supplyNode.Id.IntegerValue, loopBackNode.Id.IntegerValue)
			});
			int indoorLoopCount = this.GetIndoorLoopCount(supplyNode);
			if (indoorLoopCount != 1)
			{
				if (indoorLoopCount == 2)
				{
					Log.Info(new string[]
					{
						"IndoorLoop count is 2;"
					});
					indoorSystem.SupplyPipe = this.GetLoopMainPipe(ref supplyNode, 2, true);
					indoorSystem.SupplyPipe.Name = "户内总供水干管";
					Log.Info(new string[]
					{
						string.Format("{0} endNode is {1}", indoorSystem.SupplyPipe.Name, indoorSystem.SupplyPipe.EndNode.Id.IntegerValue)
					});
					indoorSystem.BackPipe = this.GetLoopMainPipe(ref loopBackNode, 2, true);
					indoorSystem.BackPipe.Name = "户内总回水干管";
					Log.Info(new string[]
					{
						string.Format("{0} endNode is {1}", indoorSystem.BackPipe.Name, indoorSystem.BackPipe.EndNode.Id.IntegerValue)
					});
					SearchNode loopBackNode2 = this.GetLoopBackNode(supplyNode.NextNode, loopBackNode.PreviousNode);
					IndoorLoop indoorLoop = this.GetIndoorLoop(supplyNode.NextNode, loopBackNode2);
					indoorLoop.Index = 1;
					indoorSystem.Add(indoorLoop);
					Log.Info(new string[]
					{
						string.Format("{0} EquipmentLoop count is {1}.........", indoorLoop.Name, indoorLoop.EquipmentLoops.Count)
					});
					SearchNode loopBackNode3 = this.GetLoopBackNode(supplyNode.BranchNode, loopBackNode.PreviousNode);
					IndoorLoop indoorLoop2 = this.GetIndoorLoop(supplyNode.BranchNode, loopBackNode3);
					indoorLoop2.Index = 2;
					indoorSystem.Add(indoorLoop2);
					Log.Info(new string[]
					{
						string.Format("{0} EquipmentLoop count is {1}.........", indoorLoop2.Name, indoorLoop2.EquipmentLoops.Count)
					});
				}
			}
			else
			{
				IndoorLoop indoorLoop3 = this.GetIndoorLoop(supplyNode, loopBackNode);
				indoorLoop3.Index = 1;
				indoorSystem.Add(indoorLoop3);
				Log.Info(new string[]
				{
					string.Format("{0} EquipmentLoop count is {1}.........", indoorLoop3.Name, indoorLoop3.EquipmentLoops.Count)
				});
			}
			return indoorSystem;
		}

		private SearchNode GetLoopBackNode(SearchNode supply, SearchNode backNode)
		{
			int connectorCount = backNode.NextNode.ConnectorCount;
			SearchNode nextNode = backNode.NextNode;
			switch (connectorCount)
			{
			case 2:
				if (this.JustEndEquipments(supply, nextNode.NextNode) == 1)
				{
					return nextNode.NextNode;
				}
				break;
			case 3:
				if (this.JustEndEquipments(supply, nextNode.BranchNode) == 1)
				{
					return nextNode.BranchNode;
				}
				if (this.JustEndEquipments(supply, nextNode.NextNode) == 1)
				{
					return nextNode.NextNode;
				}
				break;
			case 4:
				if (this.JustEndEquipments(supply, nextNode.BranchNode) == 1)
				{
					return nextNode.BranchNode;
				}
				if (this.JustEndEquipments(supply, nextNode.Branch2Node) == 1)
				{
					return nextNode.Branch2Node;
				}
				break;
			}
			return null;
		}

		private IndoorLoop GetIndoorLoop(SearchNode supply, SearchNode back)
		{
			IndoorLoop indoorLoop = new IndoorLoop();
			int count = (from n in supply.GetEndNodes()
			where this.equipmentsId.Contains(n.Id)
			select n).ToList<SearchNode>().Count;
			bool flag = true;
			for (int i = 1; i <= count; i++)
			{
				HyCalcPipe loopMainPipe = this.GetLoopMainPipe(ref supply, count + 1 - i, flag);
				loopMainPipe.Index = i;
				loopMainPipe.Name = string.Format("{0}干管{1}", supply.SystemClassName(), i);
				Log.Info(new string[]
				{
					string.Format("{0} endNode is {1}", loopMainPipe.Name, loopMainPipe.EndNode.Id.IntegerValue)
				});
				indoorLoop.SupplyPipes.Add(loopMainPipe);
				EquipmentLoop loopEquipmentLoop = this.GetLoopEquipmentLoop(ref flag, supply, null);
				supply = (flag ? supply.BranchNode : supply.NextNode);
				loopEquipmentLoop.Index = i;
				indoorLoop.Add(loopEquipmentLoop);
			}
			for (int j = 1; j <= count; j++)
			{
				HyCalcPipe loopMainPipe2 = this.GetLoopMainPipe(ref back, count + 1 - j, flag);
				loopMainPipe2.Index = j;
				loopMainPipe2.Name = string.Format("{0}干管{1}", back.SystemClassName(), j);
				Log.Info(new string[]
				{
					string.Format("{0} endNode is {1}", loopMainPipe2.Name, loopMainPipe2.EndNode.Id.IntegerValue)
				});
				indoorLoop.BackPipes.Add(loopMainPipe2);
				if (indoorLoop.ConnType == 0 || this.GetLoopEquipmentLoop(ref flag, back, indoorLoop.GetAt(j)) == null)
				{
					indoorLoop.ConnType = 0;
					this.GetLoopEquipmentLoop(ref flag, back, indoorLoop.GetAt(count + 1 - j));
				}
				back = (flag ? back.BranchNode : back.NextNode);
				Log.Info(new string[]
				{
					"indoorLoop conntype is {0}".Format(new object[]
					{
						indoorLoop.ConnType
					})
				});
			}
			return indoorLoop;
		}

		private HyCalcPipe GetLoopMainPipe(ref SearchNode node, int lastCount, bool bSide = false)
		{
			HyCalcPipe hyCalcPipe = new HyCalcPipe();
			this.NodeAddToPipe(ref hyCalcPipe, node.PreviousNode, bSide);
			while ((lastCount == 1 && !node.NextNode.IsRiserPipe()) || (lastCount != 1 && node.ConnectorCount < 3))
			{
				this.NodeAddToPipe(ref hyCalcPipe, node);
				node = node.NextNode;
			}
			hyCalcPipe.EndNode = node.PreviousNode;
			return hyCalcPipe;
		}

		private EquipmentLoop GetLoopEquipmentLoop(ref bool bSide, SearchNode node, EquipmentLoop loop = null)
		{
			HyCalcPipe hyCalcPipe = new HyCalcPipe();
			if (node.ConnectorCount == 2)
			{
				hyCalcPipe.Add(new ValveAndFitting(node.Element, ValveAndFittingType.Elbow));
				node = node.NextNode;
			}
			else if (node.ConnectorCount == 3)
			{
				List<SearchNode> endNodes = node.BranchNode.GetEndNodes();
				List<SearchNode> endNodes2 = node.NextNode.GetEndNodes();
				if (endNodes.Count == 1 && (loop == null || endNodes.First<SearchNode>().Id.Equals(loop.Equipment.Id)))
				{
					hyCalcPipe.Add(new ValveAndFitting(node.Element, ValveAndFittingType.SideTee));
					node = node.BranchNode;
					bSide = false;
				}
				else
				{
					if (endNodes2.Count != 1 || (loop != null && !endNodes2.First<SearchNode>().Id.Equals(loop.Equipment.Id)))
					{
						return null;
					}
					hyCalcPipe.Add(new ValveAndFitting(node.Element, ValveAndFittingType.DirectTee));
					node = node.NextNode;
					bSide = true;
				}
			}
			while (node.ConnectorCount > 1 && node.NextNode != null)
			{
				this.NodeAddToPipe(ref hyCalcPipe, node);
				node = node.NextNode;
			}
			hyCalcPipe.Name = node.SystemClassName() + "管";
			hyCalcPipe.EndNode = node.PreviousNode;
			Log.Element(node.Element);
			EquipmentLoop equipmentLoop;
			if ((equipmentLoop = loop) == null)
			{
				equipmentLoop = new EquipmentLoop(node.Element, (node.In == null) ? node.PreviousNode.Out.Radius : node.In.Radius, 1);
			}
			loop = equipmentLoop;
			loop.Add(hyCalcPipe, node.SystemClassName() == "供水");
			Log.Info(new string[]
			{
				string.Format("equipmentLoop elementId is {0}", loop.Equipment.Id.IntegerValue)
			});
			return loop;
		}

		private int GetIndoorLoopCount(SearchNode node)
		{
			int result = 1;
			while (node.ConnectorCount < 3)
			{
				node = node.NextNode;
			}
			int connectorCount = node.ConnectorCount;
			if (connectorCount != 3)
			{
				if (connectorCount == 4)
				{
					result = 1;
				}
			}
			else if (node.NextNode.GetEndNodes().Count == 1 || node.BranchNode.GetEndNodes().Count == 1)
			{
				result = 1;
			}
			else
			{
				result = 2;
			}
			return result;
		}

		private bool NodeAddToPipe(ref HyCalcPipe hyPipe, SearchNode node)
		{
			switch (node.ElementType())
			{
			case SearchElementType.PIPE:
			{
				if (node.CurFlow(this.equipmentsId) == 0.0)
				{
					return false;
				}
				PipeLine pipe = new PipeLine(node.Element as Pipe, node.CurFlow(this.equipmentsId));
				hyPipe.Add(pipe);
				break;
			}
			case SearchElementType.VALVE:
			{
				ValveAndFitting vf = new ValveAndFitting(node.Element, ValveAndFittingType.Gate);
				hyPipe.Add(vf);
				break;
			}
			case SearchElementType.ELBOW:
			{
				ValveAndFitting vf2 = new ValveAndFitting(node.Element, ValveAndFittingType.Elbow);
				hyPipe.Add(vf2);
				break;
			}
			case SearchElementType.Expand:
			{
				ValveAndFitting vf2 = new ValveAndFitting(node.Element, ValveAndFittingType.Expand);
				hyPipe.Add(vf2);
				break;
			}
			case SearchElementType.Narrow:
			{
				ValveAndFitting vf2 = new ValveAndFitting(node.Element, ValveAndFittingType.Narrow);
				hyPipe.Add(vf2);
				break;
			}
			}
			return true;
		}

		private void NodeAddToPipe(ref HyCalcPipe hyPipe, SearchNode node, bool bSide)
		{
			ValveAndFittingType tp = ValveAndFittingType.Other;
			switch (node.ConnectorCount)
			{
			case 2:
				tp = ValveAndFittingType.Elbow;
				break;
			case 3:
				if (node.SystemClassName() == "供水")
				{
					tp = (bSide ? ValveAndFittingType.SideTee : ValveAndFittingType.DirectTee);
				}
				else
				{
					tp = (bSide ? ValveAndFittingType.ShuntTee : ValveAndFittingType.DirectTee);
				}
				break;
			case 4:
				tp = (bSide ? ValveAndFittingType.STCorss : ValveAndFittingType.DirectCross);
				break;
			}
			hyPipe.Add(new ValveAndFitting(node.Element, tp));
		}

		public void SetPipeTemp(HYHeatingSystemOption option)
		{
			if (this.buildingSystem.SupplyPipes.Count == 0)
			{
				return;
			}
			this.SetPipeTemp(this.buildingSystem.SupplyPipes, (double)option.SupplyTemperature, option);
			this.SetPipeTemp(this.buildingSystem.BackPipes, (double)option.ReturnTemperature, option);
			Log.Info(new string[]
			{
				"main pipe count is " + this.buildingSystem.SupplyPipes.Count
			});
            //Action<PipeLine> <>9__0;
            //Action<PipeLine> <>9__1;
            //Action<PipeLine> <>9__2;
            //Action<PipeLine> <>9__3;
            //foreach (RisePipeSystem risePipeSystem in this.buildingSystem.RisePipes)
            //{
            //    Log.Info(new string[]
            //    {
            //        string.Format("{0} floor count is {1}", risePipeSystem.Name, risePipeSystem.SupplyPipes.Count)
            //    });
            //    this.SetPipeTemp(risePipeSystem.SupplyPipes, (double)option.SupplyTemperature, option);
            //    this.SetPipeTemp(risePipeSystem.BackPipes, (double)option.ReturnTemperature, option);
            //    foreach (FloorSystem floorSystem in risePipeSystem.FloorSystems)
            //    {
            //        Log.Info(new string[]
            //        {
            //            string.Format("{0} indoor count is {1}", floorSystem.Name, floorSystem.InDoorSystem.Count)
            //        });
            //        if (floorSystem.SupplyPipe != null)
            //        {
            //            floorSystem.SupplyPipe.SetFix(option);
            //            IEnumerable<PipeLine> pipesLines = floorSystem.SupplyPipe.PipesLines;
            //            Action<PipeLine> action;
            //            if ((action = <>9__0) == null)
            //            {
            //                action = (<>9__0 = delegate(PipeLine p)
            //                {
            //                    p.Temperature = (double)option.SupplyTemperature;
            //                    p.CalcResistance((double)option.SupplyTemperature, (double)option.ReturnTemperature);
            //                });
            //            }
            //            pipesLines.ForEach(action);
            //            floorSystem.BackPipe.SetFix(option);
            //            IEnumerable<PipeLine> pipesLines2 = floorSystem.BackPipe.PipesLines;
            //            Action<PipeLine> action2;
            //            if ((action2 = <>9__1) == null)
            //            {
            //                action2 = (<>9__1 = delegate(PipeLine p)
            //                {
            //                    p.Temperature = (double)option.SupplyTemperature;
            //                    p.CalcResistance((double)option.SupplyTemperature, (double)option.ReturnTemperature);
            //                });
            //            }
            //            pipesLines2.ForEach(action2);
            //        }
            //        foreach (IndoorSystem indoorSystem in floorSystem.InDoorSystem)
            //        {
            //            Log.Info(new string[]
            //            {
            //                string.Format("{0} indoorloop count is {1}", indoorSystem.Name, indoorSystem.Loops.Count)
            //            });
            //            if (indoorSystem.SupplyPipe != null)
            //            {
            //                indoorSystem.SupplyPipe.SetFix(option);
            //                IEnumerable<PipeLine> pipesLines3 = indoorSystem.SupplyPipe.PipesLines;
            //                Action<PipeLine> action3;
            //                if ((action3 = <>9__2) == null)
            //                {
            //                    action3 = (<>9__2 = delegate(PipeLine p)
            //                    {
            //                        p.Temperature = (double)option.SupplyTemperature;
            //                        p.CalcResistance((double)option.SupplyTemperature, (double)option.ReturnTemperature);
            //                    });
            //                }
            //                pipesLines3.ForEach(action3);
            //                indoorSystem.BackPipe.SetFix(option);
            //                IEnumerable<PipeLine> pipesLines4 = indoorSystem.BackPipe.PipesLines;
            //                Action<PipeLine> action4;
            //                if ((action4 = <>9__3) == null)
            //                {
            //                    action4 = (<>9__3 = delegate(PipeLine p)
            //                    {
            //                        p.Temperature = (double)option.ReturnTemperature;
            //                        p.CalcResistance((double)option.SupplyTemperature, (double)option.ReturnTemperature);
            //                    });
            //                }
            //                pipesLines4.ForEach(action4);
            //            }
            //            foreach (IndoorLoop indoorLoop in indoorSystem.Loops)
            //            {
            //                Log.Info(new string[]
            //                {
            //                    string.Format("{0} EquipmentLoops count is {1}", indoorLoop.Name, indoorLoop.EquipmentLoops.Count)
            //                });
            //                foreach (EquipmentLoop equipmentLoop in indoorLoop.EquipmentLoops)
            //                {
            //                    this.SetPipeTemp(equipmentLoop.SupplyPipeSet, (double)option.SupplyTemperature, option);
            //                    this.SetPipeTemp(equipmentLoop.BackPipeSet, (double)option.ReturnTemperature, option);
            //                }
            //            }
            //        }
            //    }
			//}
		}

		private void SetPipeTemp(PipeSet pSet, double dTemp, HYHeatingSystemOption option)
		{
            //Action<PipeLine> <>9__1;
            //pSet.ForEach(delegate(HyCalcPipe pipe)
            //{
            //    IEnumerable<PipeLine> pipesLines = pipe.PipesLines;
            //    Action<PipeLine> action;
            //    if ((action = <>9__1) == null)
            //    {
            //        action = (<>9__1 = delegate(PipeLine p)
            //        {
            //            p.Temperature = dTemp;
            //            p.CalcResistance((double)option.SupplyTemperature, (double)option.ReturnTemperature);
            //        });
            //    }
            //    pipesLines.ForEach(action);
            //    pipe.SetFix(option);
            //});
		}

		private BuildingSystem buildingSystem;

		private Document doc;

		private SearchSystem searchSupplySystem;

		private SearchSystem searchBackSystem;

		private List<ElementId> equipmentsId;

		private string searchFilePath = Product.UserDataLocation + "\\HeatingSearch.xml";
	}
}
